﻿package mortal.game.view.drugBack
{
    import Message.BroadCast.*;
    import Message.DB.Tables.*;
    import Message.Game.*;
    import Message.Public.*;
    import __AS3__.vec.*;
    import com.gengine.debug.*;
    import com.gengine.global.*;
    import extend.language.*;
    import flash.events.*;
    import mortal.common.net.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.ai.ais.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.cd.*;
    import mortal.game.view.guide.*;
    import mortal.game.view.shopMall.data.*;
    import mortal.game.view.systemSetting.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class DrugPackController extends Controller
    {
        private var _DrugModule:DrugPackModule;
        private var _currentTabIndex:int;
        private var _lastLifeDrugUid:String;
        private var _lastManaDrugUid:String;
        private var _lastPetDrugUid:String;

        public function DrugPackController()
        {
            this.init();
            return;
        }// end function

        private function init() : void
        {
            this.itemListUpdateHandler();
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._DrugModule == null)
            {
                this._DrugModule = new DrugPackModule();
                this._DrugModule.addEventListener(WindowEvent.SHOW, this.onWinShow);
                this._DrugModule.addEventListener(WindowEvent.CLOSE, this.onWinClose);
            }
            return this._DrugModule;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsAdd, this.useDrug);
            NetDispatcher.addCmdListener(ServerCommand.SystemClientSettingGot, this.setDrugAutoUsePercent);
            NetDispatcher.addCmdListener("buff_" + EBuffType._EBuffTypeIncOrDeCureInterval, this.resetTotalCdTime);
            NetDispatcher.addCmdListener("buff_" + EBuffType._EBuffTypeIncOrDeManaInterval, this.resetTotalCdTime);
            NetDispatcher.addCmdListener(ServerCommand.PetAttributeUpdate, this.usePetLifeDrug);
            NetDispatcher.addCmdListener(ServerCommand.PetInfoUpdate, this.usePetLifeDrug);
            Dispatcher.addEventListener(EventName.OpenDrugWin, this.openWin);
            Dispatcher.addEventListener(EventName.AutoUseDrugItem, this.autoUseDrugPackByType);
            Dispatcher.addEventListener(EventName.LifeDrugUse, this.useLifeDrug);
            Dispatcher.addEventListener(EventName.ManaDrugUse, this.useManaDrug);
            Dispatcher.addEventListener(EventName.PetLifeDrugUse, this.usePetLifeDrug);
            Dispatcher.addEventListener(EventName.InstantBackMedicineCDClear, this.instantBackMedicineCDClear);
            Dispatcher.addEventListener(EventName.AutoFightAIStatusChange, this.useAllDrug);
            return;
        }// end function

        private function onWinShow(event:Event) : void
        {
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.refreshItem);
            NetDispatcher.addCmdListener(ServerCommand.DrugUpdata, this.refreshDrugNum);
            return;
        }// end function

        private function onWinClose(event:Event) : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.refreshItem);
            NetDispatcher.removeCmdListener(ServerCommand.DrugUpdata, this.refreshDrugNum);
            return;
        }// end function

        private function instantBackMedicineCDClear(event:DataEvent) : void
        {
            var _loc_3:ItemData = null;
            var _loc_2:Array = [];
            _loc_3 = DrugPackUtil.getCdKeyByType(EDrug._EDrugLifeInstant);
            _loc_2.push(_loc_3);
            this.clearDrugCd(_loc_2);
            return;
        }// end function

        private function clearDrugCd(param1:Array) : void
        {
            var _loc_2:ICDData = null;
            var _loc_3:* = undefined;
            for each (_loc_3 in param1)
            {
                
                _loc_2 = Cache.instance.cd.getCDData(_loc_3, CDDataType.itemData);
                if (_loc_2)
                {
                    _loc_2.stopCoolDown();
                }
            }
            return;
        }// end function

        private function useDrug(param1:Object) : void
        {
            var _loc_2:* = param1[0] as ItemData;
            var _loc_3:* = param1[1];
            if (ItemsUtil.isDrug(_loc_2))
            {
                switch(_loc_2.itemInfo.type)
                {
                    case EDrug._EDrugLife:
                    {
                        this.useLifeDrug();
                        break;
                    }
                    case EDrug._EDrugMana:
                    {
                        this.useManaDrug();
                        break;
                    }
                    case EDrug._EDrugPetLife:
                    {
                        this.usePetLifeDrug();
                        break;
                    }
                    case EDrug._EDrugLifeBag:
                    {
                        if (_loc_3 == UpdateCode.EUpdateCodeTaskAndGuide)
                        {
                            return;
                        }
                        this.autoUseDrugPackByType(EDrug._EDrugLifeBag);
                        break;
                    }
                    case EDrug._EDrugManaBag:
                    {
                        if (_loc_3 == UpdateCode.EUpdateCodeTaskAndGuide)
                        {
                            return;
                        }
                        this.autoUseDrugPackByType(EDrug._EDrugManaBag);
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            return;
        }// end function

        private function autoUseDrugPackByType(param1:Object) : void
        {
            var _loc_2:ItemData = null;
            var _loc_3:int = 0;
            var _loc_4:GuideUseItemData = null;
            if (param1 is DataEvent)
            {
                _loc_3 = int(param1.data);
            }
            else
            {
                _loc_3 = int(param1);
            }
            switch(_loc_3)
            {
                case EDrug._EDrugLifeBag:
                {
                    if (Cache.instance.DrugPack.liftData.leftNum == 0)
                    {
                        _loc_2 = Cache.instance.pack.backPackCache.getBestProByTypeAndCategory(EProp._EPropDrug, EDrug._EDrugLifeBag);
                        if (_loc_2)
                        {
                            if (true || ClientSetting.local.getIsDone(IsDoneType.AutoUseLifeDrug))
                            {
                                if (this._lastLifeDrugUid && _loc_2.serverData.uid == this._lastLifeDrugUid)
                                {
                                    return;
                                }
                                Dispatcher.dispatchEvent(new DataEvent(EventName.BackPack_Use, _loc_2));
                            }
                            else
                            {
                                _loc_4 = new GuideUseItemData();
                                _loc_4.itemData = _loc_2;
                                _loc_4.isGuideToUseItem = true;
                                _loc_4.isCountdown = false;
                                _loc_4.showTxt = Language.getString(35284);
                                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideShowBackPackItem, _loc_4));
                            }
                        }
                    }
                    break;
                }
                case EDrug._EDrugManaBag:
                {
                    if (Cache.instance.DrugPack.manaData.leftNum == 0)
                    {
                        _loc_2 = Cache.instance.pack.backPackCache.getBestProByTypeAndCategory(EProp._EPropDrug, EDrug._EDrugManaBag);
                        if (_loc_2)
                        {
                            if (true || ClientSetting.local.getIsDone(IsDoneType.AutoUseManaDrug))
                            {
                                if (this._lastManaDrugUid && _loc_2.serverData.uid == this._lastManaDrugUid)
                                {
                                    return;
                                }
                                Dispatcher.dispatchEvent(new DataEvent(EventName.BackPack_Use, _loc_2));
                            }
                            else
                            {
                                _loc_4 = new GuideUseItemData();
                                _loc_4.itemData = _loc_2;
                                _loc_4.isGuideToUseItem = true;
                                _loc_4.isCountdown = false;
                                _loc_4.showTxt = Language.getString(35285);
                                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideShowBackPackItem, _loc_4));
                            }
                        }
                    }
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function addItemCDHandler(param1:Object) : void
        {
            var _loc_2:* = param1 as ItemData;
            if (_loc_2.itemInfo.cdTime <= 0)
            {
                return;
            }
            var _loc_3:* = CDCache.getKey(_loc_2.itemInfo);
            var _loc_4:* = cache.cd.registerCDData(CDDataType.itemData, _loc_3);
            _loc_4.totalTime = _loc_2.itemInfo.cdTime * 1000;
            return;
        }// end function

        private function itemListUpdateHandler(param1:Object = null) : void
        {
            var _loc_4:ICDData = null;
            var _loc_5:ICDData = null;
            this.addItemCDHandler(DrugPackUtil.getCdKeyByType(EDrug._EDrugLifeBag));
            this.addItemCDHandler(DrugPackUtil.getCdKeyByType(EDrug._EDrugManaBag));
            this.addItemCDHandler(DrugPackUtil.getCdKeyByType(EDrug._EDrugPetLifeBag));
            var _loc_2:* = Cache.instance.cd.getCDData(DrugPackUtil.getCdKeyByType(EDrug._EDrugLifeBag), CDDataType.itemData);
            _loc_2.totalTime = 12000;
            _loc_2.addFinishCallback(this.useLifeDrug);
            var _loc_3:* = Cache.instance.cd.getCDData(DrugPackUtil.getCdKeyByType(EDrug._EDrugManaBag), CDDataType.itemData);
            _loc_3.totalTime = 12000;
            _loc_3.addFinishCallback(this.useManaDrug);
            _loc_4 = Cache.instance.cd.registerCDData(CDDataType.backPackLock, "AutoUseLifeDrug", _loc_4);
            _loc_4.totalTime = 5000;
            _loc_4.addFinishCallback(this.useLifeDrug);
            _loc_5 = Cache.instance.cd.registerCDData(CDDataType.backPackLock, "AutoUseManaDrug", _loc_5);
            _loc_5.totalTime = 5000;
            _loc_5.addFinishCallback(this.useManaDrug);
            this.useLifeDrug();
            this.useManaDrug();
            this.usePetLifeDrug();
            return;
        }// end function

        public function resetTotalCdTime(param1:Object) : void
        {
            var _loc_3:ICDData = null;
            var _loc_2:* = param1 as int;
            if (_loc_2 == EBuffType._EBuffTypeIncOrDeCureInterval)
            {
                _loc_3 = Cache.instance.cd.getCDData(DrugPackUtil.getCdKeyByType(EDrug._EDrugLifeBag), CDDataType.itemData);
                _loc_3.totalTime = 12000 + Cache.instance.buff.getBuffTypeValue(EBuffType._EBuffTypeIncOrDeCureInterval) * 1000;
            }
            else if (_loc_2 == EBuffType._EBuffTypeIncOrDeManaInterval)
            {
                _loc_3 = Cache.instance.cd.getCDData(DrugPackUtil.getCdKeyByType(EDrug._EDrugManaBag), CDDataType.itemData);
                _loc_3.totalTime = 12000 + Cache.instance.buff.getBuffTypeValue(EBuffType._EBuffTypeIncOrDeManaInterval) * 1000;
            }
            return;
        }// end function

        private function callLaterUseLifeDrug(param1 = null) : void
        {
            CallLater.setCallLater(this.useLifeDrug, 1);
            return;
        }// end function

        private function useLifeDrug(param1 = null) : void
        {
            var cdData:ICDData;
            var autoUseCdData:ICDData;
            var itemData:ItemData;
            var tshopSell:TShopSell;
            var shopData:ShopItemData;
            var e:* = param1;
            var entityInfo:* = Cache.instance.role.roleEntityInfo.entityInfo;
            if (entityInfo.life > 0 && entityInfo.life < Cache.instance.DrugPack.liftData.autoNum * entityInfo.maxLife)
            {
                cdData = Cache.instance.cd.getCDData(DrugPackUtil.getCdKeyByType(EDrug._EDrugLifeBag), CDDataType.itemData);
                autoUseCdData = Cache.instance.cd.getCDData("AutoUseLifeDrug", CDDataType.backPackLock);
                if (cdData && cdData.isCoolDown)
                {
                    if (Global.isDebugModle)
                    {
                    }
                }
                else if (autoUseCdData && autoUseCdData.isCoolDown)
                {
                    if (Global.isDebugModle)
                    {
                    }
                }
                else
                {
                    if (Global.isDebugModle)
                    {
                    }
                    autoUseCdData.beginTime = 0;
                    autoUseCdData.startCoolDown();
                    if (Cache.instance.DrugPack.getDataBuyType(EDrug._EDrugLifeBag).leftNum > 0)
                    {
                        GameProxy.role.useDrugBag(EDrug._EDrugLifeBag, "");
                    }
                    else
                    {
                        itemData = Cache.instance.pack.backPackCache.getHeightestCanUseProByTypeAndCategory(EProp._EPropDrug, EDrug._EDrugLifeBag);
                        if (itemData)
                        {
                            if (itemData.serverData.itemAmount == 1)
                            {
                                if (this._lastLifeDrugUid && itemData.serverData.uid == this._lastLifeDrugUid)
                                {
                                    return;
                                }
                                var reSetUid:* = function () : void
            {
                _lastLifeDrugUid = "";
                return;
            }// end function
            ;
                                this._lastLifeDrugUid = itemData.serverData.uid;
                                CallLater.setCallLater(reSetUid, 2);
                            }
                            Dispatcher.dispatchEvent(new DataEvent(EventName.BackPack_Use, itemData));
                        }
                        else if (AutoFightAI.isWorking && Cache.instance.autoFight.isAutoBuyCXDrug)
                        {
                            if (Global.isDebugModle)
                            {
                                MsgManager.showRollTipsMsg(Language.getString(35286));
                            }
                            tshopSell = this.getHeightestCanUseDrugCode(EDrug._EDrugLifeBag);
                            shopData = new ShopItemData(tshopSell);
                            shopData.num = 1;
                            shopData.type = tshopSell.isBindMoney;
                            shopData.isUseGoldBuyGoldBind = true;
                            shopData.isShowTips = false;
                            Dispatcher.dispatchEvent(new DataEvent(EventName.BuyItem, shopData));
                        }
                    }
                }
            }
            return;
        }// end function

        private function useManaDrug(param1 = null) : void
        {
            var cdData:ICDData;
            var autoUseCdData:ICDData;
            var itemData:ItemData;
            var tshopSell:TShopSell;
            var shopData:ShopItemData;
            var e:* = param1;
            var entityInfo:* = Cache.instance.role.roleEntityInfo.entityInfo;
            if (entityInfo.mana >= 0 && entityInfo.mana < Cache.instance.DrugPack.manaData.autoNum * entityInfo.maxMana)
            {
                cdData = Cache.instance.cd.getCDData(DrugPackUtil.getCdKeyByType(EDrug._EDrugManaBag), CDDataType.itemData);
                autoUseCdData = Cache.instance.cd.getCDData("AutoUseManaDrug", CDDataType.backPackLock);
                if (cdData && cdData.isCoolDown)
                {
                    if (Global.isDebugModle)
                    {
                        Log.debug("药品cd中");
                    }
                }
                else if (autoUseCdData && autoUseCdData.isCoolDown)
                {
                    if (Global.isDebugModle)
                    {
                        Log.debug("自动调用cd中");
                    }
                }
                else
                {
                    if (Global.isDebugModle)
                    {
                        Log.debug("自动调用蓝包");
                    }
                    if (Cache.instance.DrugPack.getDataBuyType(EDrug._EDrugManaBag).leftNum > 0)
                    {
                        autoUseCdData.beginTime = 0;
                        autoUseCdData.startCoolDown();
                        GameProxy.role.useDrugBag(EDrug._EDrugManaBag, "");
                    }
                    else
                    {
                        itemData = Cache.instance.pack.backPackCache.getHeightestCanUseProByTypeAndCategory(EProp._EPropDrug, EDrug._EDrugManaBag);
                        if (itemData)
                        {
                            MsgManager.showRollTipsMsg(Language.getString(35287));
                            if (itemData.serverData.itemAmount == 1)
                            {
                                if (this._lastManaDrugUid && itemData.serverData.uid == this._lastManaDrugUid)
                                {
                                    return;
                                }
                                var reSetUid:* = function () : void
            {
                _lastManaDrugUid = "";
                return;
            }// end function
            ;
                                this._lastManaDrugUid = itemData.serverData.uid;
                                CallLater.setCallLater(reSetUid, 2);
                            }
                            Dispatcher.dispatchEvent(new DataEvent(EventName.BackPack_Use, itemData));
                        }
                        else if (AutoFightAI.isWorking && Cache.instance.autoFight.isAutoBuyCXDrug)
                        {
                            if (Global.isDebugModle)
                            {
                                MsgManager.showRollTipsMsg(Language.getString(35288));
                            }
                            tshopSell = this.getHeightestCanUseDrugCode(EDrug._EDrugManaBag);
                            shopData = new ShopItemData(tshopSell);
                            shopData.num = 1;
                            shopData.type = tshopSell.isBindMoney;
                            shopData.isUseGoldBuyGoldBind = true;
                            shopData.isShowTips = false;
                            Dispatcher.dispatchEvent(new DataEvent(EventName.BuyItem, shopData));
                        }
                    }
                }
            }
            return;
        }// end function

        private function usePetLifeDrug(param1 = null) : void
        {
            var cdData:ICDData;
            var autoUseCdData:ICDData;
            var pet:SPet;
            var itemData:ItemData;
            var tshopSell:TShopSell;
            var shopData:ShopItemData;
            var e:* = param1;
            return;
        }// end function

        private function getHeightestCanUseDrugCode(param1:int) : TShopSell
        {
            var _loc_2:ItemData = null;
            var _loc_3:ItemData = null;
            var _loc_4:TShopSell = null;
            var _loc_7:TShopSell = null;
            var _loc_5:* = ShopConfig.instance.getShopSellItemsByShopGroup(ShopConfig.fastBuyShopGroup);
            var _loc_6:* = Cache.instance.role.roleEntityInfo.entityInfo.level;
            for each (_loc_7 in _loc_5)
            {
                
                _loc_2 = new ItemData(_loc_7.itemCode);
                if (_loc_6 >= _loc_2.itemInfo.level && _loc_2.itemInfo.group == EGroup._EGroupProp && _loc_2.itemInfo.category == EProp._EPropDrug && _loc_2.itemInfo.type == param1)
                {
                    if (_loc_3 == null)
                    {
                        _loc_3 = _loc_2;
                        _loc_4 = _loc_7;
                        continue;
                    }
                    if (_loc_3.itemInfo.level < _loc_2.itemInfo.level)
                    {
                        _loc_3 = _loc_2;
                        _loc_4 = _loc_7;
                    }
                }
            }
            return _loc_4;
        }// end function

        private function useAllDrug(event:DataEvent) : void
        {
            if (event.data == true)
            {
                this.useLifeDrug();
                this.useManaDrug();
                this.usePetLifeDrug();
            }
            return;
        }// end function

        private function openWin(event:DataEvent) : void
        {
            if (!GameController.drugPack.isViewShow)
            {
                GameManager.instance.popupModuleType(ModuleType.DrugPack);
            }
            if (int(event.data) == (view as DrugPackModule)._tabBar.selectedIndex)
            {
                view.hide();
                return;
            }
            this._DrugModule.changeTabIndex(int(event.data));
            return;
        }// end function

        private function refreshItem(param1:Object = null) : void
        {
            this._DrugModule.refreshItem();
            return;
        }// end function

        private function refreshDrugNum(param1:Object = null) : void
        {
            this._DrugModule.refreshDrugNum();
            return;
        }// end function

        private function setDrugAutoUsePercent(param1:Object) : void
        {
            Cache.instance.DrugPack.liftData.autoNum = ClientSetting.local.drugPackSetting[EDrug._EDrugLifeBag] * 0.01;
            Cache.instance.DrugPack.manaData.autoNum = ClientSetting.local.drugPackSetting[EDrug._EDrugManaBag] * 0.01;
            Cache.instance.DrugPack.petLiftData.autoNum = ClientSetting.local.drugPackSetting[EDrug._EDrugPetLifeBag] * 0.01;
            return;
        }// end function

    }
}
